package com.jwd.avmanager.server;

import com.jwd.avmanager.constant.Constants;
import com.jwd.avmanager.util.DebugLogger;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class WebsocketServer implements Runnable{
    private Integer port = 7000;
    private String websocketUrl = "/ws";

    private void socketServer(){
        // 创建 boss 和 worker 工作组
        DebugLogger.info("socketServersocketServer  启动");
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup);
            // 主线程处理
            serverBootstrap.channel(NioServerSocketChannel.class);
            // 子线程业务处理
            serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) {
                    ChannelPipeline pipeline = ch.pipeline();
                    //因为基于http协议，使用http的编码和解码器
                    pipeline.addLast(new HttpServerCodec());
                    //是以块方式写，添加ChunkedWriteHandler处理器
                    pipeline.addLast(new ChunkedWriteHandler());
                    // http数据聚合器 用于将大数据量分段传输的数据 聚合
                    pipeline.addLast(new HttpObjectAggregator(10240));
                    // 自定义处理器 - 处理 web socket 文本消息
//                    pipeline.addLast(new TextWebSocketHandler());
                    // 自定义的业务处理
                    pipeline.addLast(new WebSocketHandler());
                    // websocket协议处理器
                    pipeline.addLast(new WebSocketServerProtocolHandler(Constants.DEFAULT_WEB_SOCKET_LINK2,false));
                    // 自定义的业务处理
//                    pipeline.addLast(new WebSocketHandler());
                }
            });
            // 启动服务器
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    @Override
    public void run() {
//        DebugLogger.debug("socketServer()");
        socketServer();
    }
}
